//获取应用实例
import network from '../../utils/network.js';
const app = getApp()
const baseUrl = 'http://jbc.com';
Page({
 
  data: {
    nickName: "",
    avatarUrl: "",
    casArray: [],
    roleArray:[],
    role_id:0,
    userName: '',
    mobile: '',
    Gender: 'female',
    casIndex: 0,
    userInfo:[],
    text: '获取验证码', //按钮文字
    currentTime: 61, //倒计时
    disabled: false, //按钮是否禁用
    phone: '', //获取到的手机栏中的值
    validate_code:'',
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data,
        });
        that.onGetRoleList("加载数据列表");
      }
    });
  },
  /**
  * 生命周期函数--监听页面加载
   */

  bindCasPickerChange: function (e) {
    var that = this;
    var currentProIndex = e.detail.value
    var role_id = that.data.roleArray[currentProIndex] //当前省代表的Code,当做查询级联市的parentid
      this.setData({ reply: false })
      this.setData({
        casIndex: e.detail.value,
        role_id:role_id
    })

  },


  onGetRoleList: function (message) {
    var that = this; 
    var data = { 
      shop_id:that.data.userInfo.shop_id,
    };
    network.requestLoading(baseUrl + '/api/User/get_role_list', data, message, function (res) {
      console.log(res);
      if (res.code > 0) {
        var casArray =  [];
        var roleArray =  [];
        if (res.data.length>0){
          var role_id = res.data[0].id;
          for (var i = 0; i < res.data.length; i++) {
            casArray[i] = res.data[i].name;
            roleArray[i] = res.data[i].id;
          } 
          that.setData({
            role_id: role_id,
            casArray: casArray,
            roleArray: roleArray,
          }) 
        }
      } else {
        // wx.showToast({
        //   title: res.msg,
        // });
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })

    })

  },


  //获取手机栏input中的值
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  //获取验证码input中的值
  validateInput: function (e) {
    this.setData({
      validate_code: e.detail.value
    })
  },
  //获取验证码按钮
  bindButtonTap: function () {
    var that = this;
    that.setData({
      disabled: true, //只要点击了按钮就让按钮禁用 （避免正常情况下多次触发定时器事件）
      color: '#ccc',
      // border-radius:'0',
    })

    var phone = that.data.phone;
    var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值
   
    var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空

    if (phone == '') {
      warn = "号码不能为空";
    } else if (phone.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone)) {
      warn = "手机号格式不正确";
    } else {
      var data = {phone: phone.trim()}
      var message = "正在发送";
      network.requestLoading(baseUrl + '/api/Sms/send', data, message, function (res) {
        console.log(res);
        if (res.code > 0) {
          //当手机号正确的时候提示用户短信验证码已经发送
            wx.showToast({
              title: '短信验证码已发送',
              icon: 'none',
              duration: 2000
            }); 
        }  
      }, function (res) {
        wx.showToast({
          title: '加载数据失败',
        })

      }) 

      //设置一分钟的倒计时
      var interval = setInterval(function () {
        currentTime--; //每执行一次让倒计时秒数减一
        that.setData({
          text: currentTime + 's', //按钮文字变成倒计时对应秒数

        })
        //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字
        if (currentTime <= 0) {
         clearInterval(interval)
          that.setData({
            text: '重新发送',
            currentTime: 61,
            disabled: false,
            color: '#0090eb'
          })
        }
      }, 1000);

    };

    //判断 当提示错误信息文字不为空 即手机号输入有问题时提示用户错误信息 并且提示完之后一定要让按钮为可用状态 因为点击按钮时设置了只要点击了按钮就让按钮禁用的情况
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })

      that.setData({
        disabled: false,
        color: '#0090eb'
      })
      return;

    };
  },


  // 提交表单
  formSubmit: function (e) {
    var that = this; 
    if (e.detail.value.phone.length == 0) {
      that.onShowToast("电话号码不得为空");
      wx.showToast({

        title: '电话号码不得为空!',

        icon: 'loading',

        duration: 1500

      })

      setTimeout(function () {

        wx.hideToast()

      }, 2000)

    } else if (e.detail.value.phone.length != 11) {

      wx.showToast({

        title: '请输入11位手机号码!',

        icon: 'loading',

        duration: 1500

      })

      setTimeout(function () {

        wx.hideToast()

      }, 2000)

    }else if (e.detail.value.validate_code.length ==0) {

      wx.showToast({

        title: '验证码不能为空!',

        icon: 'loading',

        duration: 1500

      })

      setTimeout(function () {

        wx.hideToast()

      }, 2000)  

    } else {

      wx.request({

        url: baseUrl + "/api/User/editRole",

        header: {

          // "Content-Type": "application/x-www-form-urlencoded"
          'Content-Type': 'application/json'

        },

        method: "POST",

        data: {
          member_id: that.data.userInfo.user_id,
          role_id: that.data.role_id,
          phone: e.detail.value.phone,
          validate_code: e.detail.value.validate_code,
        },

        success: function (res) {
          console.log(res);
          return false;
          if (res.data.status == 0) {

            wx.showToast({

              title: res.data.info,

              icon: 'loading',

              duration: 1500

            })

          } else {

            wx.showToast({

              title: res.data.info,//这里打印出登录成功

              icon: 'success',

              duration: 1000

            })

          }

        }
      })
    }
  },


 
})
